package com.llt.ai.controller;

import lombok.RequiredArgsConstructor;
import org.bouncycastle.util.encoders.UTF8;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

/**
 * @Author: llt
 * @Date: 2025-08-06 上午9:50
 */
@RestController
@RequiredArgsConstructor
public class AiAgentController {

    private final ChatClient chatClient;

    @CrossOrigin
    @GetMapping(value = "/ai", produces = "text/event-stream;charset=UTF-8")
    public ResponseEntity<Flux<String>> ask(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, "text/event-stream;charset=UTF-8")
                .header(HttpHeaders.CACHE_CONTROL, "no-cache")
                .header(HttpHeaders.CONNECTION, "keep-alive")
                .body(chatClient.prompt()
                        .user(message)
                        .system(p -> p.param("current_date", LocalDate.now()))
                        .stream()
                        .content());
    }
}
